package chapter9;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-20 下午1:40
 */
public class TicketThread3 implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(TicketThread.class);
    private int ticket = 5;

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            this.sale();
        }
    }

    private synchronized void sale() {
        if (this.ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LOGGER.info("[{}] Sell out one ticket, left tickets-{}", Thread.currentThread().getName(), ticket--);
        }
    }
}
